package com.ruoyi.common.enums;

import java.util.HashMap;
import java.util.Map;

import org.springframework.lang.Nullable;

/**
 * 请求方式
 *
 * @author ruoyi
 */
public enum HttpMethod {
    GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE;

    /**
     * HTTP方法名称到枚举值的映射缓存
     * 用于快速查找对应的HTTP方法枚举
     */
    private static final Map<String, HttpMethod> mappings = new HashMap<>(16);

    /**
     * 初始化HTTP方法映射表
     * 将所有枚举值按名称存入映射表中，提高查找效率
     */
    static {
        for (HttpMethod httpMethod : values()) {
            mappings.put(httpMethod.name(), httpMethod);
        }
    }

    /**
     * 根据HTTP方法名称解析对应的枚举值
     *
     * @param method HTTP方法名称，如"GET"、"POST"等
     * @return 对应的HttpMethod枚举值，如果找不到或输入为null则返回null
     */
    @Nullable
    public static HttpMethod resolve(@Nullable String method) {
        return (method != null ? mappings.get(method) : null);
    }

    /**
     * 判断当前枚举值是否与指定的HTTP方法名称匹配
     *
     * @param method HTTP方法名称
     * @return 如果当前枚举值与指定方法名称匹配则返回true，否则返回false
     */
    public boolean matches(String method) {
        return (this == resolve(method));
    }
}
